import os

from flask import Blueprint, render_template, request

from dc_shop.admin_panel.app.constants import media_path

upload_bp = Blueprint("upload_bp", __name__, template_folder="templates")


@upload_bp.route("/admin/upload", methods=["POST", "GET"])
def upload():
    if request.method == "POST":
        f = request.files.get("file")
        file_path = os.path.join(media_path, f.filename)
        f.save(file_path)
        # You can return a JSON response then get it on client side:
        # (see template index.html for client implementation)
        # return jsonify(uploaded_path=file_path)
    return render_template("file/upload.html")
